package com.eims.hongbao.fragment;

import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.ImageManager;
import com.eims.sls_app.view.XCRoundImageView;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import bls.juejin.cn.R;

/**
 * �Զ��������� Created by xuboyu on 2017/2/20.
 */

public class LuckeyDialog extends Dialog {

	public LuckeyDialog(Context context) {
		super(context);
	}

	public LuckeyDialog(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String name;
		private String url;
		private String des;

		private Button red_page;

		private TextView tvDes;
		private TextView tvLingJiangjinList;

		private XCRoundImageView iv_Head;
		private TextView tvSendDes;
		private TextView tvTishiShow;

		private String openButtonText;
		private OnClickListener openButtonClickListener;

		private String closeButtonText;
		private OnClickListener closeButtonClickListener;

		private String sy, yl;

		public Builder(Context context, int dialog) {
			this.context = context;
		}

		/**
		 * Set the Dialog title from resource
		 * 
		 * @param name
		 * @return
		 */
		public Builder setName(int name) {
			this.name = (String) context.getText(name);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 * 
		 * @param name
		 * @return
		 */

		public Builder setName(String name) {
			this.name = name;
			return this;
		}

		public Builder setURl(String url) {
			this.url = url;
			return this;
		}

		public Builder setDEs(String des) {
			this.des = des;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 * 
		 * @param closeButtonText
		 * @return
		 */
		public Builder setCloseButton(int closeButtonText, OnClickListener listener) {
			this.closeButtonText = (String) context.getText(closeButtonText);
			this.closeButtonClickListener = listener;
			return this;
		}

		public void SetDes(String dse,String limit) {

			
			
			if (dse.equals("未跟单")) {
				red_page.setVisibility(View.GONE);
				tvDes.setVisibility(View.VISIBLE);
				tvLingJiangjinList.setVisibility(View.GONE);
				tvTishiShow.setVisibility(View.GONE);
				tvDes.setText("跟单"+limit+"元后可抢红包");
			}else {
				red_page.setVisibility(View.GONE);
				tvDes.setVisibility(View.VISIBLE);
				tvLingJiangjinList.setVisibility(View.VISIBLE);
				
				tvDes.setText(dse);
				if (dse.contains("元")) {
					tvTishiShow.setVisibility(View.VISIBLE);
				}else {
					tvTishiShow.setVisibility(View.GONE);
				}
			}
			
			
			

		}



		public Builder SetSy(String sy, String yl) {

			this.sy = sy;
			this.yl = yl;
			return this;

		}

		public Builder setCloseButton(String closeButtonText, OnClickListener listener) {
			this.closeButtonText = closeButtonText;
			this.closeButtonClickListener = listener;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 * 
		 * @param openButtonText
		 * @return
		 */
		public Builder setOpenButton(int openButtonText, OnClickListener listener) {
			this.openButtonText = (String) context.getText(openButtonText);
			this.openButtonClickListener = listener;
			return this;
		}

		public Builder setOpenButton(String openButtonText, OnClickListener listener) {
			this.openButtonText = openButtonText;
			this.openButtonClickListener = listener;
			return this;
		}

		public LuckeyDialog create() {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			final LuckeyDialog dialog = new LuckeyDialog(context, R.style.Dialog);
			View layout = inflater.inflate(R.layout.main_kaihongbao_dialog, null);

			iv_Head = (XCRoundImageView) layout.findViewById(R.id.head_img);

			ImageManager.getInstance().displayImage(App.url + url, iv_Head, ImageManager.getViewsHeadOptions());
			tvSendDes = (TextView) layout.findViewById(R.id.textView2);
			tvSendDes.setText(des);

			tvLingJiangjinList = (TextView) layout.findViewById(R.id.tv_lingjiang_list);
			red_page = (Button) layout.findViewById(R.id.open_btn);

			tvDes = (TextView) layout.findViewById(R.id.tv_open_btn);
			tvTishiShow=(TextView) layout.findViewById(R.id.tv_open_tishi);
			dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT));

			((TextView) layout.findViewById(R.id.name)).setText(name);
			

			if (yl.equals("0")) {

				if (sy.equals("0")) {
					tvLingJiangjinList.setVisibility(View.VISIBLE);
					red_page.setVisibility(View.GONE);
					tvDes.setVisibility(View.VISIBLE);
					tvTishiShow.setVisibility(View.GONE);
					tvDes.setText("红包已被抢完");
				} else {
					
					
					tvLingJiangjinList.setVisibility(View.GONE);
					red_page.setVisibility(View.VISIBLE);
					tvDes.setVisibility(View.GONE);
					tvTishiShow.setVisibility(View.GONE);
				}

			} else {
				tvLingJiangjinList.setVisibility(View.VISIBLE);
				red_page.setVisibility(View.GONE);
				tvDes.setVisibility(View.VISIBLE);
				tvTishiShow.setVisibility(View.GONE);
				tvDes.setText("您已抢过此红包");

			}
			if (openButtonText != null) {
				((Button) layout.findViewById(R.id.open_btn)).setText(openButtonText);
				if (openButtonClickListener != null) {
					((Button) layout.findViewById(R.id.open_btn)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							AnimationDrawable animationDrawable = (AnimationDrawable) red_page.getBackground();
							animationDrawable.start();// ��������</span>
							openButtonClickListener.onClick(dialog, 1);
						}
					});
					((TextView) layout.findViewById(R.id.tv_lingjiang_list))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {

									openButtonClickListener.onClick(dialog, 2);
								}
							});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.open_btn).setVisibility(View.GONE);
			}

			// ���ùرհ�ť
			if (closeButtonText != null) {
				((Button) layout.findViewById(R.id.close)).setText(closeButtonText);
				if (closeButtonClickListener != null) {
					((Button) layout.findViewById(R.id.close)).setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {

							closeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
						}
					});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				layout.findViewById(R.id.close).setVisibility(View.GONE);
			}

			dialog.setContentView(layout);
			return dialog;
		}

	}

}